<template>
  <!-- 系统管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="任务名称:" prop="name" v-if="!parentId">
            <el-input
              v-model.trim="filterForm.name"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入任务名称"
              @keyup.enter.native="$_handleFilter"
            />
          </el-form-item>
          <el-form-item label="执行状态:" prop="status">
            <el-select v-model.trim="filterForm.status" placeholder="请选择" :clearable="true">
              <el-option
                v-for="item in [{value: -1,label:'执行失败'}, {value: 0,label:'执行中'}, {value: 1,label:'执行成功'}]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始执行时间:" prop="times">
            <el-date-picker
              v-model="filterForm.times"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束执行时间:" prop="eTimes">
            <el-date-picker
              v-model="filterForm.eTimes"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-button type="primary" @click="$_handleFilter">查询</el-button>
          <el-button @click="$_handleReset">重置</el-button>
        </el-form>

      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="MX_listLoading"
          :data="MX_list"
          border
          highlight-current-row
          :row-key="$_getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="MX_headerCellStyle"
        >
          <template slot="empty">
            <p>暂无数据!</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="$_tableIndex"
          />
          <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :min-width="item.minWidth"
            :formatter="item.formatter"
            align="center"
            show-overflow-tooltip
          />
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="MX_total > 0"
          :total="MX_total"
          :page.sync="MX_pageNav.pageNo"
          :limit.sync="MX_pageNav.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>
<script>
import commonList from "@/utils/mixins/investment/commonList";
import dataCaptureApi from "@/api/investment/dataCapture";
import {parseTime} from "@/framework/utils/date";

export default {
  name: 'ExecutionLog',
  mixins: [commonList],
  data() {
    return {
      parentId: null,
      filterForm: {
        name: '',
        status: '',
        times: '',
        eTimes: ''
      },
      // 列信息
      columns: [
        { prop: 'name', label: `任务名称`, minWidth: '180px' },
        { prop: 'startTime', label: `开始时间`, width: '180px', formatter: function(row) {
            return !!row.startTime ? parseTime(new Date(row.startTime)) : ''
          } },
        { prop: 'endTime', label: `结束时间`, width: '180px', formatter: function(row) {
            return !!row.endTime ? parseTime(new Date(row.endTime)) : ''
          } },
        { prop: 'timeConsuming', label: `耗时（s）`, width: '120px' },
        { prop: 'dataNumber', label: `处理条数`, width: '120px' },
        { prop: 'status', label: `执行状态`, width: '140px', formatter: function(row) {
            let tText = '';
            switch (row.status){
              case -1:
                tText = '执行失败'
                break;
              case 0:
                tText = '执行中'
                break;
              case 1:
                tText = '执行成功'
                break;
            }
            return tText
          } },
        { prop: 'errRemark', label: `失败报错`, minWidth: '140px' }
      ],
      idKeyName: 'id',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      }
    }
  },
  watch: {
    parentId(val, old){
      if (val !== old){
        this.getList()
        this.$set(this.filterForm, 'name', '')
      }
    },
    'filterForm.times'(val){
      this.$set(this.filterForm, 'sStart', !!val ? val[0] : '')
      this.$set(this.filterForm, 'sEnd', !!val ? val[1] : '')
    },
    'filterForm.eTimes'(val){
      this.$set(this.filterForm, 'eStart', !!val ? val[0] : '')
      this.$set(this.filterForm, 'eEnd', !!val ? val[1] : '')
    },
  },
  created() {
    this.parentId = this.$route.query.id
  },
  activated() {
    this.parentId = this.$route.query.id
  },
  methods: {
    // 获取页面列表
    getList() {
      let filterForm = JSON.parse(JSON.stringify(this.filterForm))
      delete filterForm.times
      delete filterForm.eTimes
      filterForm = {
        ...filterForm,
        ...this.MX_pageNav,
      }
      if (!!this.parentId){
        filterForm = {
          id: this.parentId,
          ...filterForm
        }
      }
      this.MX_listLoading = true
      dataCaptureApi.getELogList(filterForm).then((res) => {
        this.MX_listLength = res.data.records.length
        this.MX_list = res.data.records
        this.MX_total = parseInt(res.data.total || '0')
        this.MX_listLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tBtn{
  margin-top: 3px;
  margin-bottom: 3px;
}
</style>
